﻿using UnityEngine;
using System.Collections;
using UniLua;

namespace TCG.FFI
{
	/// <summary>
	/// 将UnityEngine.GameObject导出为lua可用的模块。
	/// 
	/// 未实现：
	/// * none
	/// 
	/// 注意：
	/// * FindObjectsOfType : 该函数会向lua写回一个table
	/// 
	/// </summary>
	public static class UnityObjectLib
	{
		#region stuff for exporting to lua
		public static string LIB_NAME = "UnityObjectLib";
		
		#endregion
		
		
		#region static variables
        // nothing here.
		#endregion
		
		
		#region variables

		public static int mf_GetHideFlags (ILuaState luaState)
		{
			UnityEngine.Object obj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushInteger ((int)(obj.hideFlags));
			return 1;
		}

		public static int mf_GetName (ILuaState luaState)
		{
			UnityEngine.Object obj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushString (obj.name);
			return 1;
		}

		public static int mf_SetName (ILuaState luaState)
		{
			UnityEngine.Object obj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			string name = luaState.ToString (2);
			obj.name = name;
			return 1;
		}
		
		#endregion
		
		
		#region constructors
		// nothing here.
		#endregion
		
		
		#region functions

		public static int mf_GetHashCode (ILuaState luaState)
		{
			UnityEngine.Object obj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushInteger (obj.GetHashCode ());
			return 1;
		}

		public static int mf_GetInstanceID (ILuaState luaState)
		{
			UnityEngine.Object obj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushInteger (obj.GetInstanceID ());
			return 1;
		}

		public static int mf___string (ILuaState luaState)
		{
			UnityEngine.Object obj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, -1, LIB_NAME);
			luaState.PushString (obj.ToString ());
			return 1;
		}

		public static int mf___eq (ILuaState luaState)
		{
			UnityEngine.Object v_1 = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			UnityEngine.Object v_2 = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, 2, LIB_NAME);
			luaState.PushBoolean (v_1 == v_2);
			return 1;
		}

		#endregion
		
		
		#region static functions

		public static int lf_Destroy (ILuaState luaState)
		{
			UnityEngine.Object targetobj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			float time = 0;
			if (luaState.GetTop () == 2)
			{
				time = (float)luaState.L_CheckNumber (2);
			}
			UnityEngine.Object.Destroy (targetobj, time);
			return 0;
		}

		public static int lf_DestroyImmediate (ILuaState luaState)
		{
			UnityEngine.Object targetobj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			UnityEngine.Object.DestroyImmediate (targetobj);
			return 0;
		}

		public static int lf_DontDestroyOnLoad (ILuaState luaState)
		{
			UnityEngine.Object targetobj = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			UnityEngine.Object.DontDestroyOnLoad (targetobj);
			return 0;
		}

		public static int lf_FindObjectOfType (ILuaState luaState)
		{
			System.Type t = (System.Type)luaState.ToUserData (1);
			UnityEngine.Object result = UnityEngine.Object.FindObjectOfType (t);
			if (result == null)
			{
				luaState.PushNil ();
			}
			else
			{
				LuaTools.L_PushUserData (luaState, result, LIB_NAME);
			}
			return 1;
		}

		public static int lf_FindObjectsOfType (ILuaState luaState)
		{
			System.Type t = (System.Type)luaState.ToUserData (1);
			UnityEngine.Object[] results = UnityEngine.Object.FindObjectsOfType (t);
			if (results == null || results.Length == 0)
			{
				luaState.PushNil ();
			}
			else
			{
				luaState.CreateTable (results.Length, 0);
				for (int i = 0, len = results.Length; i < len; ++ i)
				{
					LuaTools.L_PushUserData (luaState, results[i], LIB_NAME);
					luaState.RawSetI (-2, i + 1);
				}
			}
			return 1;
		}

		public static int lf_Instantiate (ILuaState luaState)
		{
			UnityEngine.Object source = (UnityEngine.Object)LuaTools.L_CheckUData (luaState, 1, LIB_NAME);
			UnityEngine.Object result = UnityEngine.Object.Instantiate (source);
			LuaTools.L_PushUserData (luaState, result, LIB_NAME);
			return 0;
		}
		
		#endregion
	}
}
